<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Global Mall</title>
    <link rel="icon" href="assets/image/logo/favicon.png" type="image/png" sizes="32x32">
    <link href="assets/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="assets/css/owl.carousel.min.css">
    <link rel="stylesheet" type="text/css" href="assets/css/owl.theme.default.min.css">
    <link rel="stylesheet" type="text/css" href="assets/fonts/css2.css">
    <link rel="stylesheet" type="text/css" href="assets/css/jquery.fancybox.min.css">
    <link rel="stylesheet" type="text/css" href="assets/css/all.min.css">
    <link rel="stylesheet" type="text/css" href="assets/css/style.css">
    <link rel="stylesheet" type="text/css" href="assets/css/responsive.css">
</head>

<body>
    <div class="preloader"></div>

    <?php
    // 引入数据库连接
    include 'conn.php';
    include 'header.php';
    if (!isset($_SESSION['user_id'])) {
        echo "<script>alert('请先登录！');window.location.href='login.php';</script>";
        exit;
    }

    // 查询所有新闻数据
    $sql = "SELECT * FROM news ORDER BY created_at DESC";
    $result = $conn->query($sql);
    ?>

    <div class="mb-3 mt-3">
        <div class="container">
            <div class="row">
                <div class="col-12">
                    <ul class="breadcums">
                        <li class="d-inline-block font-weight-bolder button-group2">
                            <a href="index.php" class="login_txt symbol2 pro_home"></a>
                        </li>
                        <li class="d-inline-block hr_ font-weight-bolder"><a href="#" class="login_txt">新 闻 资 讯</a></li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <div class="news-container">
        <?php
        if ($result->num_rows > 0) {
            // 输出每条新闻
            while ($row = $result->fetch_assoc()) {
                $news_title = $row['title'];
                $news_image = $row['image'] ? $row['image'] : 'assets/image/Blog/default.jpg';
                $news_content = substr($row['content'], 0, 150) . '...';  // 取部分内容
                $news_date = date('d M, Y', strtotime($row['created_at']));
                $news_view_count = $row['view_count'];
                $news_id = $row['id'];
        ?>
                <a href="news.detail.php?id=<?php echo $news_id; ?>" class="news-item">
                    <img src="<?php echo $news_image; ?>" alt="新闻图片">
                    <div class="news-info">
                        <h3><?php echo $news_title; ?></h3>
                        <p><?php echo $news_content; ?></p>
                        <div class="meta">
                            <span>发布于 <?php echo $news_date; ?></span>
                            <span>浏览量: <?php echo $news_view_count; ?></span>
                        </div>
                    </div>
                </a>
        <?php
            }
        } else {
            echo "<p>没有找到新闻</p>";
        }
        ?>
    </div>

    <?php
    include 'footer.php';
    ?>

    <script src="assets/js/jquery-3.4.1.min.js"></script>
    <script src="assets/js/owl.carousel.js"></script>
    <script src="assets/js/popper.min.js"></script>
    <script src="assets/js/bootstrap.min.js"></script>
    <script src="assets/js/jquery.fancybox.min.js"></script>
    <script src="assets/js/custom.js"></script>
    <script src="https://unpkg.com/aos@2.3.1/dist/aos.js"></script>
</body>

</html>